import axios from "axios";
import { BASE_URL } from "../config/http.js";

function getToken() {
  return sessionStorage.getItem("token") || "";
}

// 获取单个用户信息
export function getUserById(userId) {
  return axios
    .get(`${BASE_URL}/api/user/${userId}`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 创建审批申请
export function createHomeApproval(data) {
  return axios
    .post(`${BASE_URL}/api/homeApproval/`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 获取当前用户的所有审批申请
export function getUserHomeApprovals(userId) {
  return axios
    .get(`${BASE_URL}/api/homeApproval/user/${userId}`, {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    })
    .then((res) => res.data);
}

export function updateHomeApproval(id, data) {
  return axios
    .put(`${BASE_URL}/api/homeApproval/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 新增书本申请
export function createBookApproval(data) {
  return axios
    .post(`${BASE_URL}/api/approveBook/`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询当前用户所有书本申请
export function getUserBookApprovals(userId) {
  return axios
    .get(`${BASE_URL}/api/approveBook/user/${userId}`, {
      headers: {
        Authorization: "Bearer " + getToken(),
      },
    })
    .then((res) => res.data);
}

// 更新书本申请
export function updateBookApproval(id, data) {
  return axios
    .put(`${BASE_URL}/api/approveBook/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 创建角色申请
export function createCharacter(data) {
  return axios
    .post(`${BASE_URL}/api/approveCharacters`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}

// 查询某用户的所有角色
export function getUserCharacters(userId) {
  return axios
    .get(`${BASE_URL}/api/approveCharacters/user/${userId}`, {
      headers: { Authorization: "Bearer " + getToken() },
    })
    .then((res) => res.data);
}

// 更新角色信息申请
export function updateCharacter(id, data) {
  return axios
    .put(`${BASE_URL}/api/approveCharacters/${id}`, data, {
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "application/json",
      },
    })
    .then((res) => res.data);
}
